﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using RabbitMQ.Client;
using RabbitMQ.Client.Framing;

namespace Kugar.Tool.RemoteEventBus.Helpers
{
    internal static class RabbitMQHelper
    {
        public static void BasicPublish(this IModel channel, 
            string exchange, 
            string routeKey, 
            string serviceID,
            string relyTo,
            object data,
            string messageID="",
            int timeout =15000
            )
        {
            if (string.IsNullOrEmpty(messageID))
            {
                messageID = Guid.NewGuid().ToString();
            }

            channel.BasicPublish(exchange,
                routeKey,//_nodeSubscibeRouteKey,
                true,
                new BasicProperties()
                {
                    CorrelationId = serviceID,
                    AppId = "RemoteEventBus",
                    Expiration = timeout.ToString(),
                    MessageId = messageID,
                    ReplyTo = relyTo,
                },
                Encoding.UTF8.GetBytes(
                    JsonConvert.SerializeObject(data)
                    )
                );
        }
    }
}
